package com.rfid.emergencywarehouse.presenter;

import com.rfid.base.base.BasePresenter;
import com.rfid.base.util.LogUtils;
import com.rfid.emergencywarehouse.entity.KW;
import com.rfid.emergencywarehouse.entity.KWAsset;
import com.rfid.emergencywarehouse.entity.Order;
import com.rfid.emergencywarehouse.net.NetApi;
import com.rfid.emergencywarehouse.net.entity.EnterWorkArea;
import com.rfid.emergencywarehouse.net.entity.KFKWRes;
import com.rfid.emergencywarehouse.net.entity.OrderKwRes;
import com.rfid.emergencywarehouse.net.entity.OrderRes;
import com.rfid.emergencywarehouse.net.entity.ShelfingRes;
import com.rfid.emergencywarehouse.view.IOnShelfWithOrderView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ShelfingWithOrderPresenter extends ShelfingPresenter<IOnShelfWithOrderView> {


    public void getOrderList() {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    List<Order> orders = new ArrayList<>();
                    OrderRes res = NetApi.INSTANCE.getOrderList();
                    for (OrderRes.DataBean bean:res.getData()) {
                        Order order = new Order();
                        order.setCreateTime(bean.getCreateTime());
                        order.setCreateUserName(bean.getCreateUserName());
                        order.setOriginOrderNo(bean.getOriginOrderNo());
                        order.setId(bean.getId());
                        order.setState(bean.getState());
                        order.setToWorkAreaId(bean.getToWorkAreaId());
                        order.setToWorkAreaName(bean.getToWorkAreaName());
                        orders.add(order);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onOrderList(orders);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getOrderKws(String orderId) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    OrderKwRes res = NetApi.INSTANCE.getInOrderKw(orderId);
                    List<KW> kws = new ArrayList<>();
                    for (OrderKwRes.DataBean bean : res.getData()) {
                        String fromLocationId = bean.getToContainerId();
                        KWAsset KWAsset = new KWAsset();
                        KWAsset.setDisplayName(bean.getDisplayName());
                        KWAsset.setState(bean.getState());
                        KWAsset.setMaterialName(bean.getMaterialName());
                        KWAsset.setPlanCount(bean.getPlanCount());
                        KWAsset.setRealCount(bean.getRealCount());
                        KWAsset.setSkuModel(bean.getSkuModel());
                        boolean add = false;
                        for (KW kw : kws) {
                            if (kw.getKw().equals(fromLocationId)) {
                                kw.getAssets().add(KWAsset);
                                add = true;
                                break;
                            }
                        }
                        if (!add) {
                            KW kw = new KW();
                            kw.setKw(fromLocationId);
                            List<KWAsset> kwAssets = new ArrayList<>();
                            kwAssets.add(KWAsset);
                            kw.setAssets(kwAssets);
                            kws.add(kw);
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mView != null) {
                                mView.onRecommendKws(kws);
                            }
                        }
                    });
                } catch (Exception e) {
                    LogUtils.e(e);
                }
            }
        });
    }

    public void postEnterStatus(String workAreaId,String outOrderId) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                EnterWorkArea req = new EnterWorkArea();
                req.setWorkAreaId(workAreaId);
                req.setInOrderId(outOrderId);
                try {
                    NetApi.INSTANCE.postEnterStatus(req);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void exitWorkArea(String workAreaId,String outOrderId) {
        mExcutor.execute(new Runnable() {
            @Override
            public void run() {
                EnterWorkArea req = new EnterWorkArea();
                req.setWorkAreaId(workAreaId);
                req.setInOrderId(outOrderId);
                try {
                    NetApi.INSTANCE.postEnterStatus(req);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
